package com.tio.tiowebscoketdemo.server;

import com.tio.tiowebscoketdemo.config.CaseServerConfig;
import com.tio.tiowebscoketdemo.handler.WebSocketMessageHandler;
import com.tio.tiowebscoketdemo.listener.ShowcaseIpStatListener;
import com.tio.tiowebscoketdemo.listener.ShowcaseServerAioListener;
import org.springframework.stereotype.Component;
import org.tio.server.ServerTioConfig;
import org.tio.utils.jfinal.P;
import org.tio.websocket.server.WsServerStarter;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @author tanyaowu
 * 2017年6月28日 下午5:34:04
 */
@Component
public class ShowcaseWebsocketStarter {

	private WsServerStarter wsServerStarter;
	private ServerTioConfig serverTioConfig;

	/**
	 *
	 * @author tanyaowu
	 */
	public ShowcaseWebsocketStarter() throws Exception {
		wsServerStarter = new WsServerStarter(CaseServerConfig.PORT, WebSocketMessageHandler.me);

		serverTioConfig = wsServerStarter.getServerTioConfig();
		serverTioConfig.setName(CaseServerConfig.PROTOCOL_NAME);
		serverTioConfig.setServerAioListener(ShowcaseServerAioListener.me);

		//设置ip监控
		serverTioConfig.setIpStatListener(ShowcaseIpStatListener.me);
		//设置ip统计时间段
		serverTioConfig.ipStats.addDurations(CaseServerConfig.IpStatDuration.IPSTAT_DURATIONS);
		
		//设置心跳超时时间
		serverTioConfig.setHeartbeatTimeout(CaseServerConfig.HEARTBEAT_TIMEOUT);

	}

	/**
	 * @author tanyaowu
	 * @throws IOException
	 */
	@PostConstruct
	public void start() throws Exception {
		this.wsServerStarter.start();
	}

	/**
	 * @return the serverTioConfig
	 */
	public ServerTioConfig getServerTioConfig() {
		return serverTioConfig;
	}

	public WsServerStarter getWsServerStarter() {
		return wsServerStarter;
	}


}
